﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace virtuaPol.Controls
{
    /// <summary>
    /// Interaction logic for MediaPlayer.xaml
    /// </summary>
    public partial class MediaPlayer : UserControl
    {
        public event EventHandler Skipped;
        public event EventHandler Stopped;

        private string filename;
        public string Filename {
            get { return filename; }
            set {
                filename = value;
                MediaElement.Source =
                    new Uri(filename, UriKind.RelativeOrAbsolute);
            }
        }

        private bool isFullscreen;
        public bool IsFullscreen {
            get { return isFullscreen; }
            set {
                isFullscreen = value;
                if (isFullscreen) {
                    this.VerticalAlignment = VerticalAlignment.Stretch;
                    this.HorizontalAlignment = HorizontalAlignment.Stretch;
                    this.Margin = new Thickness(0);
                    this.Width = double.NaN;
                    this.Height = double.NaN;
                } else {
                    this.VerticalAlignment = VerticalAlignment.Center;
                    this.HorizontalAlignment = HorizontalAlignment.Center;
                    this.Margin = new Thickness(0);
                    this.Width = 600;
                    this.Height = 400;
                }
            }
        }

        public MediaPlayer(string filename, bool fullscreen)
        {
            InitializeComponent();
            IsFullscreen = fullscreen;
            Filename = filename;
        }

        public void Play()
        {
            MediaElement.Play();
        }

        private void BtSkip_Click(object sender, RoutedEventArgs e)
        {
            MediaElement.Stop();
            if (Stopped != null) Stopped(this, new EventArgs());
            if (Skipped != null) Skipped(this, new EventArgs());
        }

        private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
        {
            if (Stopped != null) Stopped(this, new EventArgs());
        }

        private void MediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
        {
            if (Stopped != null) Stopped(this, new EventArgs());
        }
    }
}
